
import React from "react";
import {Table, message, Modal,Button} from 'antd';
import store from "../../pages/generate/ordinary/Store";
import service from "../../libs/request";


class ActionDiv extends React.Component{
    constructor(props) {
        super(props);
        this.state = {}
    }

    showActionType = (type,configUri,configParams={},config={}) => {
        if (configParams === "selected_ids"){
            if (store.selected_ids.length === 0){
                message.warning("请选择需要进行操作的数据行");
                return true;
            }else {
                configParams = {"selected_ids":JSON.stringify(store.selected_ids)}
            }
        }
        //初始化参数
        if (type === "MODAL"){
            store.showModal(configUri,configParams,config);
        }else if (type === "CONFIRM"){
            console.log("ActionDiv",configUri,configParams,config);
            Modal.confirm({
                title: config.title ? config.title : "执行操作确认",
                content: config.content ? config.content : "确定要执行该操作吗?",
                onOk: () => {
                    return service.request({
                        url:configUri,
                        method:"get",
                        params:configParams,
                    }).then(response => {
                        if (response.data.code === 200){
                            store.selected_ids = [];
                            console.log(store.selected_ids)
                            message.success(response.data.msg ? response.data.msg : "操作成功");
                            store.fetchRecords()
                        }
                    })
                }
            });
        }else if (type === "EXECUTE"){
            service.request({
                url:configUri,
                method:"get",
                params:configParams,
            }).then(response => {
                console.log(response.data);
                if (response.data.code === 200){
                    message.success(response.data.msg ? response.data.msg : "操作成功");
                    store.fetchRecords()
                }
            })
        }else if (type === "EXPORT"){ //批量导出类型
            const columns = [
                {
                    title: '筛选项',
                    dataIndex: 'label',
                    key: 'label',
                },
                {
                    title: '筛选值',
                    dataIndex: 'value',
                    key: 'value',
                }
            ];
            Modal.confirm({
                title: "导出结果确认",
                content: (
                    <Table style={{marginTop:20}} dataSource={store.getSearchResult()} columns={columns} pagination={false}/>
                ),
                onOk: () => {
                    //先探测一下有没有数据，因为arraybuffer类型请求后无法判断返回数据的状态
                    if (store.records.length === 0){
                        message.error("没有可导出的数据")
                    }else {
                        //深拷贝对象
                        let searchMapClone = Object.assign({},store.searchMap);
                        delete searchMapClone["page"];
                        delete searchMapClone["pageSize"];
                        delete searchMapClone["sort"];
                        return service.request({
                            url:configUri,
                            method:"get",
                            responseType:'arraybuffer',
                            params:searchMapClone,
                        }).then(response => {
                            let blob = new Blob([response.data]);
                            let downloadElement = document.createElement('a');
                            let href = window.URL.createObjectURL(blob); //创建下载的链接
                            downloadElement.href = href;
                            downloadElement.download = config.fileName + "." + config.format; //下载后文件名
                            document.body.appendChild(downloadElement);
                            downloadElement.click(); //点击下载
                            document.body.removeChild(downloadElement); //下载完成移除元素
                            window.URL.revokeObjectURL(href); //释放掉blob对象
                            message.success("导出成功")
                        })
                    }
                }
            });
        } else {
            message.error("不支持的操作类型:" + type)
        }
    };

    render() {
        const {config} = this.props;
        return (
            <div style={{marginBottom: 16}} {...this.props}>
                {
                    config.map((item,index) => {
                        if (item.auth){
                            if (index === 0){
                                return <Button key={index} type={item["type"] ? item["type"] : "primary"} icon={item["icon"]} onClick={() => this.showActionType(item["actionType"],item["configUri"],item["configParams"],item["config"])}>{item["label"] ? item["label"] : "操作按钮"}</Button>
                            }else {
                                return <Button key={index} style={{marginLeft: 20}} type={item["type"] ? item["type"] : "primary"} icon={item["icon"]} onClick={() => this.showActionType(item["actionType"],item["configUri"],item["configParams"],item["config"])}>{item["label"] ? item["label"] : "操作按钮"}</Button>
                            }
                        }
                        return true;
                    })
                }
            </div>
        );
    }
}

export default ActionDiv;
